package com.example.katalk_nari;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class ChatItemAdapter extends BaseAdapter {

	private Context mContext;
	private List<ChatItemData> mDatas;
	
	public ChatItemAdapter(Context c, List<ChatItemData> datas) {
		mContext = c;
		mDatas = datas;
	}
	
	@Override
	public int getViewTypeCount() {
		
		return ChatItemData.TYPE_COUNT;
	}
	
	@Override
	public int getItemViewType(int position) {
		ChatItemData data = mDatas.get(position);
		return data.getMessageType();
	}
	
	@Override
	public int getCount() {		
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ChatItemData data = mDatas.get(position);
		
		switch(data.getMessageType()){
		case ChatItemData.TYPE_SEND:			
			SendMessageview sendItemview;
			
			if (convertView == null) {
				sendItemview = new SendMessageview(mContext);
			} else {
				sendItemview = (SendMessageview)convertView;
			}			
			sendItemview.setData((SendMessageData)data);
			return sendItemview;
			
		case ChatItemData.TYPE_RECIVE:			
			ReciveMessageview reciveItemView;
			
			if (convertView == null) {
				reciveItemView = new ReciveMessageview(mContext);
			} else {
				reciveItemView = (ReciveMessageview)convertView;
			}			
			reciveItemView.setData((ReciveMessageData)data);
			return reciveItemView;
			
		case ChatItemData.TYPE_DATE:
			TimeBarview dateItemView;
			
			if (convertView == null) {
				dateItemView = new TimeBarview(mContext);
			} else {
				dateItemView = (TimeBarview)convertView;
			}			
			dateItemView.setData((TimeBarData)data);
			return dateItemView;
		}			
		
		return convertView;
	}

	public void add(ChatItemData data){
		mDatas.add(data);
		notifyDataSetChanged();
	}
}